<?php

namespace app\modules\home\controllers;

use yii\web\Controller;
use app\models\LoginForm;
use Yii;
use yii\base\Model;
use yii\helpers\Url;
use \app\components\API;
use app\components\login_model;
use yii\captcha\CaptchaAction;

class PublicController extends Controller {

    public $layout = 'main';
    public $error = null;
 
    /**
     * 临时关闭验证
     */
//   public $enableCsrfValidation = false;
    public function actionIndex() {
        
    }

    public function actionLogin() {
        
        $Enabled_Captcha = \app\models\Log::checkLoginFail();   //是否启用验证码,如果最近有登录失败的记录，返回true
        
        $this->layout = false;
        \app\components\Lcms::decrypt();
        if (\Yii::$app->request->post('LoginForm')) {
            if (!isset(\Yii::$app->request->post('LoginForm')['username'], \Yii::$app->request->post('LoginForm')['password'])) {
                API::echo_json(1, '参数错误');
            }
           // $verification = $_POST['LoginForm']['verification'];
            $model = new LoginForm();
            $model->username =\Yii::$app->request->post('LoginForm')['username'];
            $model->password = \Yii::$app->request->post('LoginForm')['password'];
            if ($model->username === '') {
                API::$errormsg = '用户名必须填写';
                API::echo_json(1, API::$errormsg);
            }
            if ($model->password === '') {
                API::$errormsg = '密码必须填写';
                API::echo_json(1, API::$errormsg);
            }
            if ($Enabled_Captcha) {
                $ca = new \app\components\Captcha($this);
                if (!$ca->validate(\Yii::$app->request->post('LoginForm')['verification'], true)) {
                    API::echo_json(0,['msg'=>'Captcha']);
                }
            }
            if($model->login()){
                API::echo_json(0,['msg'=>'ok', 'url'=>  Url::to(['/admin/'])]);
            }else{
                //var_dump($model->getErrors());exit;
                API::$errormsg=API::tostr($model->getErrors());
                API::echo_json(0,['msg'=>'登录失败']);
            }
            /*
              if ($model->load(Yii::$app->request->post()) && $model->login()) {

              API::echo_json(0,['url'=>  Url::to(['/admin/'])]);
              } */
            exit;
        }

        return $this->render('login',['Enabled_Captcha'=>$Enabled_Captcha]);
    }

    /*
      public function actionLogin() {
      $model = new LoginForm();
      if ($model->load(Yii::$app->request->post()) && $model->login()) {

      \app\models\Log::addLoginPass();
      Yii::$app->response->redirect(
      Url::to(['/admin/view/index']));
      return;
      } else {
      if (Yii::$app->request->post() && $model->getErrors()) {
      $this->error = \app\components\Fun::getErrorArrayStr($model->getErrors());
      }
      $this->layout = false;
      return $this->render('login', [
      'model' => $model,
      ]);
      }
      } */

/////   /home/public/captcha
    public function actionCaptcha() {
   //    phpinfo();exit;
        $ca = new \app\components\Captcha($this);

        $ca->run();
/*
          $ca = new CaptchaAction('captcha', $this);
          $ca->maxLength = 4;
          $ca->minLength = 4;
          $ca->backColor = 0xEEEEEE;
          $ca->width = 80;
          $val = $ca->run();
          if (isset($_GET[CaptchaAction::REFRESH_GET_VAR])) {
             echo json_encode($val);
          } else {
            echo $val;
          }
*/
    }

    /**
     * 显示微信信息
     */
    public function actionWxauth() {
        $data = (new \app\components\Share)->getWeixinKey();

        header('Content-Type: application/x-javascript; charset=UTF-8');
        echo 'var _wx_auth ={"timestamp":"' . $data['timestamp']
        . '","appId":"' . $data['appId'] . '","nonceStr":"' .
        $data['nonceStr'] . '","signature":"' . $data['signature'] . '","url":"' . $data['url'] . '"};';
        return;
    }

    /**
     * 微信登录回调
     */
    public function actionWeixin() {
        $weixin = new \app\components\Weixin();
        $weixin->Login();
    }

 
 

    /**
     * 退出链接
     */
    public function actionLogout() {
        Yii::$app->user->logout();
        $this->redirect(
                \yii\helpers\Url::to(['/'])
        );
    }
 
    /**
     * 文件上传
     */
    public function actionFile() {
        if (Yii::$app->user->isGuest) {
            API::echo_json(API::guest);
        }
        exit;

        \app\components\UploadFile::start();
        exit;
    }
    
    public function actionQrlogin(){
        $wx=new \app\components\MicroAPP\WeixinApp();
        $wx->Agree();
        API::echo_json(0,'empty');
    }

}
